package cn.edu.hbwe.book.servlet.book;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.hbwe.book.entity.Book;

/**
 * Servlet implementation class ChangeCartServlet
 */
public class ChangeCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		//商品数量为0则移除出购物车
		HttpSession session = request.getSession();
		Map<Book,Integer> cart = (Map<Book, Integer>) session.getAttribute("cart");
		Book book = new Book();
		book.setId(id);
		if(cart.get(book).intValue()!=0) {
			int count = cart.get(book);
			cart.put(book, count);
		}else {
			cart.remove(book);
		}
		//移除购物车的商品，跳转cart.jsp
		response.sendRedirect(request.getContextPath()+"/bookweb/cart.jsp");
	}

}
